package com.zrf.base.knowledge.io;

import org.apache.commons.lang3.RandomUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * socket test
 * @date 2020/3/26
 * @AUTHER zhouruifeng
 */
public class SocketTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("start 9999");
        while (true) {
            Socket clientSocket = serverSocket.accept();
            System.out.println("client: " + clientSocket.getPort());

            new Thread(()-> {
                try {
                    InputStream inputStream = clientSocket.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    while (bufferedReader.read() != 0) {
                        System.out.println(bufferedReader.readLine());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
